4
4
.
.
3
3
.
.
8
8
T
T
e
e
s
s
t
t
-
-
R
R
e
e
s
s
p
p
o
o
n
n
s
s
e
e
-
-
B
B
o
o
d
d
y
y
-
-
J
J
S
S
O
O
N
N
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to test if Controller returns valid JSON in Response Body.
Application Schema [Result]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
Syntax
//CREATE REQUEST
MockHttpServletRequestBuilder request = get("/Hello?name=John&age=20");
//PERFORM REQUEST. RETURN RESULT.
MvcResult mvcResult = mockMvc.perform(request).andReturn();
String responseBody = mvcResult.getResponse().getContentAsString();
//GENERATE EXPECTED JSON
String expectedPersonDTOJSON = objectMapper.writeValueAsString(new PersonDTO("John", 20));
//CHECK RESPONSE BODY
assertEquals(expectedPersonDTOJSON, responseBody);
MyController
PersonDTO
MyControllerTest
http://localhost:8080/Hello?name=John&age=20
Browser
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_test_mockmvc_response_body_json (add Spring Boot Starters from the table)
Create Package: DTO (inside main package)
– Create Class: PersonDTO.java (inside package DTO)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside controllers package)
Create Test Class: MyControllerTest.java
PersonDTO.java
package com.ivoronline.springboot_test_mockmvc_response_body_json.DTO;
public class PersonDTO {
public String name;
public Integer age;
}
MyController.java
package com.ivoronline.springboot_test_mockmvc_response_body_json.controllers;
import com.ivoronline.springboot_test_mockmvc_response_body_json.DTO.PersonDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@GetMapping("/Hello")
public PersonDTO hello(@RequestParam String name, @RequestParam Integer age) {
//CREATE PERSON DTO
PersonDTO personDTO = new PersonDTO();
personDTO.name = name;
personDTO.age = age;
//RETURN SOMETHING
return personDTO;
}
}
MyControllerTest.java
package com.ivoronline.springboot_test_mockmvc_response_body_json.controllers;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ivoronline.springboot_test_mockmvc_response_body_json.DTO.PersonDTO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
class MyControllerTest {
@Autowired MockMvc mockMvc;
@Autowired MyController myController;
@Autowired ObjectMapper objectMapper;
@Test
void hello() throws Exception {
//CREATE REQUEST
MockHttpServletRequestBuilder request = get("/Hello?name=John&age=20");
//PERFORM REQUEST. RETURN RESULT.
MvcResult mvcResult = mockMvc.perform(request).andReturn();
String responseBody = mvcResult.getResponse().getContentAsString();
//CREATE EXPECTED PERSON DTO
PersonDTO expectedPersonDTO = new PersonDTO();
expectedPersonDTO.name = "John";
expectedPersonDTO.age = 20;
String expectedPersonDTOJSON = objectMapper.writeValueAsString(expectedPersonDTO);
//CHECK RESPONSE BODY
assertEquals(expectedPersonDTOJSON, responseBody);
}
}
R
R
e
e
s
s
u
u
l
l
t
t
http://localhost:8080/Hello?name=John&age=20
Wrong Response Body get("/Hello?name=John2&age=20")
org.opentest4j.AssertionFailedError:
Expected :{"name":"John","age":20}
Actual :{"name":"John2","age":20}
Run Test Class: MyControllerTest.java
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>